package com.tgh.utils;

import android.opengl.GLES20;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

public class SkyBox {

    
    public SkyBox(float size) {
        mSize=size;
        init();
    }
    
    private float mSize;
    private FloatBuffer mVertices;
    private FloatBuffer mTextureCoords;
    private FloatBuffer mColors;
    private FloatBuffer mNormals;
    private ByteBuffer mIndices;
    
    private void init(){
        float halfSize = mSize * .5f;
        
        float[] vertices = {
                halfSize, halfSize, halfSize, -halfSize, halfSize, halfSize, -halfSize,-halfSize, halfSize, halfSize,-halfSize, halfSize, //0-1-halfSize-3 front
                halfSize, halfSize, halfSize, halfSize,-halfSize, halfSize,  halfSize,-halfSize,-halfSize, halfSize, halfSize,-halfSize,//0-3-4-5 right
                halfSize,-halfSize,-halfSize, -halfSize,-halfSize,-halfSize, -halfSize, halfSize,-halfSize, halfSize, halfSize,-halfSize,//4-7-6-5 back
                -halfSize, halfSize, halfSize, -halfSize, halfSize,-halfSize, -halfSize,-halfSize,-halfSize, -halfSize,-halfSize, halfSize,//1-6-7-halfSize left
                halfSize, halfSize, halfSize, halfSize, halfSize,-halfSize, -halfSize, halfSize,-halfSize, -halfSize, halfSize, halfSize, //top
                halfSize,-halfSize, halfSize, -halfSize,-halfSize, halfSize, -halfSize,-halfSize,-halfSize, halfSize,-halfSize,-halfSize,//bottom
                };
        mVertices = GLTools.makeFloatBuffer(vertices);
        float t = 1;
        
        float[] skyboxTextureCoords = {
                -t,t,t, t,t,t, t,-t,t, -t,-t,t,     // front
                t,t,-t, t,-t,-t, t,-t,t, t,t,t,   // up
                -t,-t,-t, t,-t,-t, t,t,-t, -t,t,-t, // back
                -t,t,-t, -t,t,t, -t,-t,t,-t,-t,-t, // down
                -t,t,t, -t,t,-t, t,t,-t, t,t,t,   // right
                -t,-t,t, t,-t,t, t,-t,-t, -t,-t,-t,  // left
        };
        mTextureCoords = GLTools.makeFloatBuffer(skyboxTextureCoords);
        float[] colors = {
            1, 1, 1, 1,     1, 1, 1, 1,     1, 1, 1, 1,     1, 1, 1, 1,
            1, 1, 1, 1,     1, 1, 1, 1,     1, 1, 1, 1,     1, 1, 1, 1,
            1, 1, 1, 1,     1, 1, 1, 1,     1, 1, 1, 1,     1, 1, 1, 1,
            1, 1, 1, 1,     1, 1, 1, 1,     1, 1, 1, 1,     1, 1, 1, 1,
            1, 1, 1, 1,     1, 1, 1, 1,     1, 1, 1, 1,     1, 1, 1, 1,
            1, 1, 1, 1,     1, 1, 1, 1,     1, 1, 1, 1,     1, 1, 1, 1
        };
        mColors = GLTools.makeFloatBuffer(colors);
        float n = 1;
        
        float[] normals = {
                0, 0, n,   0, 0, n,   0, 0, n,   0, 0, n,     //front
                n, 0, 0,   n, 0, 0,   n, 0, 0,   n, 0, 0,     // right
                0, 0,-n,   0, 0,-n,   0, 0,-n,   0, 0,-n,     //back
                -n, 0, 0,  -n, 0, 0,  -n, 0, 0,  -n, 0, 0,     // left
                0, n, 0,   0, n, 0,   0, n, 0,   0, n, 0,     //  top                          
                0,-n, 0,   0,-n, 0,   0,-n, 0,   0,-n, 0,     // bottom
        };
        mNormals = GLTools.makeFloatBuffer(normals);
        byte[] skyboxIndices = {
                2,1,0, 3,2,0,
                6,5,4, 7,6,4,
                10,9,8, 11, 10, 8,
                14, 13, 12, 15, 14, 12,
                18, 17, 16, 19, 18, 16,
                22, 21, 20, 23, 22, 20
        };
        mIndices = GLTools.makeByteBuffer(skyboxIndices);
    }
    
    public void draw() {
        GLES20.glVertexAttribPointer(GLShaderManager.GL_ATTRIBUTE_VERTEX, 3, GLES20.GL_FLOAT, false, 0, mVertices);
        GLES20.glEnableVertexAttribArray(GLShaderManager.GL_ATTRIBUTE_VERTEX);
        
        GLES20.glVertexAttribPointer(GLShaderManager.GL_ATTRIBUTE_NORMAL, 3, GLES20.GL_FLOAT, false, 0, mNormals);
        GLES20.glEnableVertexAttribArray(GLShaderManager.GL_ATTRIBUTE_NORMAL);
        
        GLES20.glVertexAttribPointer(GLShaderManager.GL_ATTRIBUTE_COLOR, 4, GLES20.GL_FLOAT, false, 0, mColors);
        GLES20.glEnableVertexAttribArray(GLShaderManager.GL_ATTRIBUTE_COLOR);
        
        GLES20.glVertexAttribPointer(GLShaderManager.GL_ATTRIBUTE_TEXTURE0, 3, GLES20.GL_FLOAT, false, 0, mTextureCoords);
        GLES20.glEnableVertexAttribArray(GLShaderManager.GL_ATTRIBUTE_TEXTURE0);
        
        GLES20.glDrawElements(GLES20.GL_TRIANGLES, mIndices.capacity(), GLES20.GL_UNSIGNED_BYTE, mIndices);
    }
    
}
